package boundarys;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controllers.UserController;
import entitys.Employee;
import entitys.Reply;
import entitys.Session;
import enums.Command;
import enums.Position;
import enums.Result;
import enums.Status;

public class LoginGUI extends AbstractGUI{

	/**
	 * Login frame
	 */
	private JFrame frmCcrmlogin;
	/**
	 * User password field
	 */
	private JPasswordField passwordField;
	/**
	 * User id field
	 */
	private JTextField userIdField;
	/**
	 * host ip  
	 */
	private String host;
	/**
	 * port
	 */
	private int port;

	/**
	 * Constructor
	 * @param host
	 * @param port
	 */
	public LoginGUI(String host, int port) {
		this.host = host;
		this.port = port;
		initialize();
		initGUI(frmCcrmlogin);
		frmCcrmlogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * 
	 * @return host
	 */
	public String getHost()
	{
		return host;
	}
	
	/**
	 * 
	 * @return port
	 */
	public int getPort()
	{
		return port;
	}
	
	/**
	 * sets host
	 * @param host
	 */
	public void setHost(String host)
	{
		this.host = host;
	}
	
	/**
	 * sets port
	 * @param port
	 */
	public void setPort(int port)
	{
		this.port = port;
	}
	
	

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmCcrmlogin = new JFrame();
		frmCcrmlogin.setResizable(false);
		frmCcrmlogin.setTitle("CCRM>Login");
		frmCcrmlogin.setBounds(100, 100, 317, 156);
		frmCcrmlogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmCcrmlogin.getContentPane().setLayout(null);
		
		JButton logInBtn = new JButton("Log In");
		logInBtn.setBounds(181, 47, 104, 47);
		frmCcrmlogin.getContentPane().add(logInBtn);
		
		JLabel lblUserId = new JLabel("ID");
		lblUserId.setBounds(30, 50, 60, 14);
		frmCcrmlogin.getContentPane().add(lblUserId);
		
		JLabel lblPassword = new JLabel("PW");
		lblPassword.setBounds(30, 75, 60, 14);
		frmCcrmlogin.getContentPane().add(lblPassword);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(67, 72, 89, 20);
		frmCcrmlogin.getContentPane().add(passwordField);
		
		userIdField = new JTextField();
		userIdField.setBounds(67, 47, 89, 20);
		frmCcrmlogin.getContentPane().add(userIdField);
		userIdField.setColumns(10);
		
		/**
		 * Action Listener for Login Button
		 * sends user id and password to controller which sends request to server that runs the query to validate details
		 */
		logInBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				UserController.validateUser(userIdField.getText(), passwordField.getText());
				
			}
		});
		
		frmCcrmlogin.setVisible(true);
		
	}

	/**
	 * Reply from server
	 * Does the right action according to the command that was sent from the controller
	 */
	@Override
	public void getReply(Object r) {
		
		if(((Reply)r).command.equals(Command.LOGIN))
		{
			Employee e = (Employee) ((Reply)r).result;
			
			if(e==null)
			{
				System.out.println("id/password mismatch");
				JOptionPane.showMessageDialog(frmCcrmlogin, "ID/PW mismatch ", "CCRM Login", JOptionPane.ERROR_MESSAGE);
			}
						
			else if(e.getStatus().equals(Status.BLOCKED))
			{
				System.out.println("user is blocked");
				JOptionPane.showMessageDialog(frmCcrmlogin, "Login failed, user is blocked!", "CCRM Login", JOptionPane.ERROR_MESSAGE);
			}
			
			else if(e.getStatus().equals(Status.LOGGED))
			{
				System.out.println("user already logged in");
				JOptionPane.showMessageDialog(frmCcrmlogin, "Login failed, user already logged in!", "CCRM Login", JOptionPane.ERROR_MESSAGE);
			}
			else if (e.getPosition().equals(Position.DM))
			{
				System.out.println("id and password are correct - login successfull");
				//JOptionPane.showMessageDialog(frmCcrmlogin, "Login successfull!", "CCRM Login", JOptionPane.INFORMATION_MESSAGE);
				frmCcrmlogin.setVisible(false);
				new DMGUI(userIdField.getText(),passwordField.getText());
			}
			
			else if (e.getPosition().equals(Position.M))
			{
				System.out.println("id and password are correct - login successfull");
				//JOptionPane.showMessageDialog(frmCcrmlogin, "Login successfull!", "CCRM Login", JOptionPane.INFORMATION_MESSAGE);
				frmCcrmlogin.setVisible(false);
				new MGUI(userIdField.getText(), passwordField.getText());
			}
			
			else if (e.getPosition().equals(Position.MM))
			{
				System.out.println("id and password are correct - login successfull");
				//JOptionPane.showMessageDialog(frmCcrmlogin, "Login successfull!", "CCRM Login", JOptionPane.INFORMATION_MESSAGE);
				frmCcrmlogin.setVisible(false);
				new MMGUI(userIdField.getText(), passwordField.getText());
			}
			
			else if (e.getPosition().equals(Position.S))
			{
				System.out.println("id and password are correct - login successfull");
				//JOptionPane.showMessageDialog(frmCcrmlogin, "Login successfull!", "CCRM Login", JOptionPane.INFORMATION_MESSAGE);
				frmCcrmlogin.setVisible(false);
				new SGUI(userIdField.getText(), passwordField.getText());
			}
			
			else if (e.getPosition().equals(Position.R))
			{
				System.out.println("id and password are correct - login successfull");
				//JOptionPane.showMessageDialog(frmCcrmlogin, "Login successfull!", "CCRM Login", JOptionPane.INFORMATION_MESSAGE);
				frmCcrmlogin.setVisible(false);
				new RGUI(userIdField.getText(), passwordField.getText());
			}
			
			Session.setEmployee(e);

		}	
	}
}
